<?php session_start(); ?>
<html>
    <head>
        <title>anh</title>
        <link href="public/css/css.css" rel="stylesheet" type="text/css">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    </head>
    <body>
        <div id="wrapper">
            <div id="baner">

                <img src="public/images/logoo.png">

            </div>
            <div id='boder_menu'>
                <ul>

                    <li><a href="#">TRANG CHU</a></li>
                    <li><a href="#">NOI DUNG</a></li>
                    <li><a href="#">CHI TIET</a></li>
                    <li>     <?php if (isset($_SESSION["id"])): ?>
                            <div id="logout"><a href="logout.php">LOGOUT</a></div>
                        <?php endif; ?></li>


                </ul>
            </div>
            <div id="col_left">


                <div class="logo">

                    <a style="color: black; margin-left: 80px;">LOGIN</a>
                    <div  align="" style="margin-top: 0px;"><form method="POST">
                            <p><a style="color: black;">Username</a>
                                <input type="text" name="username" ></p>
                            <p><a style="color: black">Password</a>
                                <input type="password" name="password" ></p>
                            <p><input type="submit" name="submit" value="submit"></p>
                        </form></div>
                    <?php require_once 'users.php'; ?>
                    <?php if (isset($_POST['submit'])): ?>
                        <?php
                        $username = $_POST['username'];
                        $password = $_POST['password'];
                        $isOk = FALSE;
                        $index = -1;
                        for ($i = 0; $i < count($users); $i++) {
                            if ((strcmp($users[$i]['username'], $username) == 0) &&
                                    (strcmp($users[$i]['password'], $password) == 0)) {
                                $isOk = TRUE;
                                $index = $i;
                            }
                        }
                        if ($isOk) {
                            $_SESSION["id"] = $users[$index]["id"];
                            header("Location: http://localhost:82/ltanh-web2/index3.php?uid=" . $users[$index]['id']);
                        } else {
                            echo 'lỗi';
                        }
                        ?>
                    <?php endif; ?>
                </div>

                <?php
                require_once 'model/db2.php';
                require_once 'model/works.php';
                require_once 'model/paginationdemo.php';
                $works = new Works();
                $paginations = new Pagination();
                ?>


                <?php
                if (!isset($_GET["page"]))
                    $page = 1;
                else
                    $page = $_GET["page"];
                $list = $works->select_index($page);
                $total = $works->countWords();
                echo $paginations->Display($page, $total, null, "index3.php");
                ?><br>
                <table border="1px" align="center">
                    <tr class="hang">
                        <td><b>id</b></td>
                        <td><b>name</b></td>
                        <td><b>description</b></td>
                        <td><b>posted</b></td>
                        <td><b>Image</b></td>
                        <td><b>Edit</b></td>
                    </tr>
                    <?php foreach ($list as $item): ?>
                        <tr>
                            <td><?php echo$item['id']; ?></td>
                            <td><a href="<?php echo$item['url']; ?>" style="text-decoration: none">
                                    <?php echo$item['name']; ?></a></td>                                            
                            <td><?php echo substr($item['description'], 0, 100); ?></td>
                            <td><?php $mydate = getdate($item['posted']); ?> 
                                <?php echo "$mydate[mday] $mydate[month], $mydate[year]"; ?></td>
                            <?php if (isset($_SESSION["id"]) && $_SESSION["id"] == 0): ?>
                                <td> <img src="<?php echo $item['image']; ?>"width="200px"></td>
                                <td> <a href="upload.php?id=<?php echo $item['id']; ?>">Edit</a>
                                    <a href="del.php?id="<?php $item['id']; ?> >delete</a>



                                </td>
                            <?php endif; ?>
                        <?php endforeach; ?>
                    </tr>
                </table>
                <br>

                <?php echo $paginations->Display($page, $total, null, "index.php"); ?>
                <br>
            </div>
        </div>
        <div style="clear: both"></div>
    </div>
    <footer>
        <div id="boder">
            <table class="o">
                <tr>
                    <td>LE TUAN ANH</td>

                </tr>
            </table>
    </footer>
</div>
</div>
</body>
</html>
